local extension = Package:new("jbsheg_variation_cards", Package.CardPack)
extension.extensionName = "jbs2"
local H = require "packages/hegemony/util"
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["jbsheg_variation_cards"] = "国战应变牌堆",
}

local heg = Fk.game_modes["new_heg_mode"]
table.insertTable(heg.whitelist, {"jbsheg_variation_cards"})

extension.game_modes_whitelist = { 'nos_heg_mode', 'new_heg_mode' ,"jbsnew_heg_mode"}


local bronzeSparrowSkill = fk.CreateTriggerSkill{
  name = "#jbshegbronze_sparrow_skill",
  mute = true,
  frequency = Skill.Compulsory,

  refresh_events = {fk.AfterCardUseDeclared},  --用refresh伪装为“状态技”
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      table.find({"@gujun"}, function(mark) return data.card:getMark(mark) ~= 0 end)
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.variation = true
  end,
}
Fk:addSkill(bronzeSparrowSkill)



--印卡函数
--1234-黑桃，梅花，红桃，方库
local cardlist1=
{"duel","double_swords","drowning","dismantlement","slash","ice__slash","ice__slash","ice__slash","slash","slash","slash","spear","black_chain",
"breastplate","vine","slash","slash","bronze_sparrow","thunder__slash","savage_assault","slash","slash","slash","slash","chasing_near","iron_chain",
"archery_attack","jink","amazing_grace","peach","chitu","indulgence","peach","foresight","peach","slash","await_exhausted","dismantlement","zhuahuangfeidian",
"five_elements_fan","jink","jink","await_exhausted","axe","six_swords","jink","jink","jink","slash","jink","slash","jink"

}

local cardlist2=
{"lightning","eight_diagram","chasing_near","drowning","jueying","qinggang_sword","slash","ice__slash","analeptic","supply_shortage","nullification","iron_chain","savage_assault",
"duel","slash","known_both","known_both","slash","indulgence","thunder__slash","thunder__slash","analeptic","supply_shortage","slash","iron_chain","heg__nullification",
"god_salvation","unexpectation","unexpectation","fire__slash","kylin_bow","peach","foresight","peach","chasing_near","peach","jink","peach","jink",
"crossbow","peach","snatch","fire__slash","fire__slash","jink","jink","jink","analeptic","jink","slash","triblade","zixing"

}
local printcardpack=function(extension,namelist,tolist)
  local normalist={}
  for i=1,4 do
    normalist[i]={}
    for j=1,13 do
      table.insert(normalist[i],j)
    end 
  end
  local tolist=tolist or normalist
  for suit,numtable in ipairs(tolist) do
    for j,number in ipairs(numtable) do
      extension:addCard(Fk:cloneCard(namelist[(suit-1)*13+j], suit, number))
    
  
  end
  end

end

printcardpack(extension,cardlist1)
printcardpack(extension,cardlist2)




local variation_rule = fk.CreateTriggerSkill{
  name = "#jbshegvariation_rule",
  priority = 0.001,
  mute = true,
  events = {fk.AfterCardUseDeclared, fk.AfterCardTargetDeclared, fk.CardUsing},
  can_trigger = function (self, event, target, player, data)
    if target == player and not player.dead then
      if event == fk.AfterCardUseDeclared then
        if data.extra_data and data.extra_data.variation then return end
        if data.card:getMark("@gujun") ~= 0 then
          local kingdomMapper = H.getKingdomPlayersNum(player.room)
          return  kingdomMapper[H.getKingdom(player)]==1
        elseif data.card:getMark("@fujia") ~= 0 then
            return table.every(player.room:getOtherPlayers(player), function(p)
              return player:getHandcardNum() >= p:getHandcardNum()
            end)
        elseif data.card:getMark("@kongchao") ~= 0 then
            return player:isKongcheng()
        elseif data.card:getMark("@canqu") ~= 0 then
            return player.hp == 1
        elseif data.card:getMark("@zhuzhan") ~= 0 then
            return table.find(player.room:getOtherPlayers(player), function(p) return not p:isKongcheng() end)
          
        end
      elseif event == fk.AfterCardTargetDeclared then
        if data.extra_data and data.extra_data.variation then
          if table.find(data.card:getMarkNames(), function(name)
            return data.card:getMark(name) == Fk:translate("variation_addtarget") end) then
            return #player.room:getUseExtraTargets( data, false) > 0
          end
          if table.find(data.card:getMarkNames(), function(name)
            return data.card:getMark(name) == Fk:translate("variation_minustarget") end) then
            return #TargetGroup:getRealTargets(data.tos) > 1
          end
        end
      elseif event == fk.CardUsing then
        return data.extra_data and data.extra_data.variation
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      if data.card:getMark("@zhuzhan") ~= 0 then
        local room = player.room
        local players = table.filter(room:getOtherPlayers(player), function (p)
          return not p:isKongcheng() and not table.contains(TargetGroup:getRealTargets(data.tos), p.id)
        end)
        if #players == 0 then return end
        local winner, cards = U.askForRaceDiscard(room, players, 1, 1, false, "@zhuzhan", ".|.|.|.|.|"..data.card:getTypeString(),
        "#zhuzhan-invoke:"..player.id.."::"..data.card:getTypeString()..":"..data.card:toLogString())
        if winner then
          self.cost_data = {winner, cards}
          return true
        end
      else
        return true
      end
    elseif event == fk.AfterCardTargetDeclared then
      local room = player.room
      if table.find(data.card:getMarkNames(), function(name)
        return data.card:getMark(name) == Fk:translate("variation_addtarget") end) then
          local tos = room:askForChoosePlayers(player, room:getUseExtraTargets( data, false), 1, 1,
            "#variation_addtarget:::"..data.card:toLogString(), "variation", true)
          if #tos > 0 then
            self.cost_data = tos
            return true
          end
      end
      if table.find(data.card:getMarkNames(), function(name)
        return data.card:getMark(name) == Fk:translate("variation_minustarget") end) then
        local tos = room:askForChoosePlayers(player, TargetGroup:getRealTargets(data.tos), 1, 1,
          "#variation_minustarget:::"..data.card:toLogString(), "variation", true)
        if #tos > 0 then
          self.cost_data = tos
          return true
        end
      end
    elseif event == fk.CardUsing then
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardUseDeclared then
      if data.card:getMark("@zhuzhan") ~= 0 then
        room:throwCard(self.cost_data[2], "variation", self.cost_data[1], self.cost_data[1])
      end
      data.extra_data = data.extra_data or {}
      data.extra_data.variation = true
    elseif event == fk.AfterCardTargetDeclared then
      if table.contains(TargetGroup:getRealTargets(data.tos), self.cost_data[1]) then
        TargetGroup:removeTarget(data.tos, self.cost_data[1])
        room:sendLog{ type = "#RemoveTargetsBySkill", from = target.id, to = self.cost_data, arg = "@zhuzhan", arg2 = data.card:toLogString() }
      else
        table.insert(data.tos, self.cost_data)
        room:sendLog{ type = "#AddTargetsBySkill", from = target.id, to = self.cost_data, arg = "@zhuzhan", arg2 = data.card:toLogString() }
      end
    elseif event == fk.CardUsing then
      if table.find(data.card:getMarkNames(), function(name)
        return data.card:getMark(name) == Fk:translate("variation_disresponsive") end) then
        data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
      end
      if table.find(data.card:getMarkNames(), function(name)
        return data.card:getMark(name) == Fk:translate("variation_draw") end) then
        player:drawCards(1)
      end
      if table.find(data.card:getMarkNames(), function(name)
        return data.card:getMark(name) == Fk:translate("variation_damage") end) then
        data.additionalDamage = (data.additionalDamage or 0) + 1
      end
      if table.find(data.card:getMarkNames(), function(name)
        return data.card:getMark(name) == Fk:translate("variation_cancel") end) then
        if data.responseToEvent and data.toCard and U.hasFullRealCard(room, data.toCard) then
          room:obtainCard(player, data.toCard, true, fk.ReasonJustMove)
        end
      end
    end
  end,

  refresh_events = {fk.GamePrepared},
  can_refresh = function(self, event, target, player, data)
    return not table.contains(player.room.disabled_packs, "jbsheg_variation_cards")
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, card in ipairs(Fk.packages["jbsheg_variation_cards"].cards) do
      if table.contains({"savage_assault"}, card.name) then--南蛮
        room:setCardMark(card, "@fujia", Fk:translate("variation_minustarget"))
      elseif card.name == "unexpectation" then--出其不意
        room:setCardMark(card, "@zhuzhan", Fk:translate("variation_addtarget"))
      elseif  card.name == "chasing_near" then--逐近弃远
        room:setCardMark(card, "@zhuzhan", Fk:translate("variation_addtarget"))
      elseif  card.name == "drowning" then--水淹七军
        room:setCardMark(card, "@zhuzhan", Fk:translate("variation_addtarget"))
      elseif card.name == "slash" then--杀
        if table.contains({10,11}, card.number) and card.suit == Card.Club then
          room:setCardMark(card, "@canqu", Fk:translate("variation_addtarget"))
        elseif card.number == 4 and card.suit == Card.Club then
          room:setCardMark(card, "@kongchao", Fk:translate("variation_addtarget"))
        end
      elseif card.name == "fire__slash" and  table.contains({4}, card.number) then--火杀
          room:setCardMark(card, "@canqu", Fk:translate("variation_damage"))

      elseif card.name == "jink" then--闪
        if table.contains({7,8}, card.number)  and card.suit == Card.Diamond then
        room:setCardMark(card, "@kongchao", Fk:translate("variation_draw"))
        end
      elseif card.name == "nullification" and card.number == 11 then
          room:setCardMark(card, "@kongchao", Fk:translate("variation_draw"))
      end
    end
  end,
}

Fk:addSkill(variation_rule)


Fk:loadTranslationTable{
  ["@gujun"] = "孤军",

}

return extension